import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';

@Component({
  selector: 'app-n-to-br',
  templateUrl: './n-to-br.component.html',
  styleUrls: ['./n-to-br.component.less'],
})
export class NToBrComponent implements OnInit {
  str = `【自动通告-{策略名称}】{事件名称},事件详情如下：\n事件类型:{事件类型}\n事件分类:{事件分类}\n优先级:{优先级}\n威胁等级:{威胁等级}\n设备来源:{设备来源}\n源情报IOC:{源情报IOC}\n目的情报IOC:{目的情报IOC}\n源IP:{源IP}\n源端口:{源端口}\n目的IP:{目的IP}\n目的端口:{目的端口}\n攻击结果:{攻击结果}\n攻击特征:{攻击特征}\n置信度:{置信度}\n开始时间:{开始时间}\n结束时间:{结束时间}\n发生次数:{发生次数}\nurl链接：{url链接}  `;
  validateForm!: FormGroup;
  templateContent: string = '';

  constructor(private fb: FormBuilder) {}

  ngOnInit(): void {
    this.validateForm = this.fb.group({
      content: [null, []],
    });
    this.getData();
  }

  toHtml(str, org?) {
    if (org) {
      return str.replace(/\\n|\r\n|\n/g, '<br/>');
    }
    const lines = str.split(/\\n|\r\n|\n/);
    const truncatedLines = lines.slice(0, 3); // Get the first three lines
    let truncatedContent = truncatedLines.join('<br/>');
    if (lines.length > 3) {
      truncatedContent += '...'; // Add ellipsis if more than three lines
    }
    return truncatedContent;
  }

  // 模拟接口数据返回
  getData(){
    let resText = this.str;//假设这已经从接口获取了数据
    this.templateContent = resText.replace(/\\n/g, "\n");
    this.validateForm.patchValue({
        content: this.templateContent,
    });
  }

  submitForm(): void {
    console.log('submit', this.validateForm.value);
  }
}
